package cn.tedu.ivos.user.controller;

import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/user")
public class UserController {
    @Autowired
    UserService userService;
    @PostMapping("/login")
    public JsonResult login(@RequestBody UserLoginParam userLoginParam){
        log.debug("接收到登录请求：{}",userLoginParam);
        log.debug("开始处理登录请求");
        UserVO userVO = userService.login(userLoginParam);
        log.debug("登录成功，用户信息为：{}",userVO);
        return JsonResult.ok(userVO);
    }
    @PostMapping("/save")
    public JsonResult saveUser(@RequestBody UserSaveParam userSaveParam){
        log.debug("用户信息：{}",userSaveParam);
        log.debug("开始处理用户注册的请求");
        if(userSaveParam.getId()==null){
            int rows = userService.saveUser(userSaveParam);
            return JsonResult.ok("保存成功");
        }else{
            int rows = userService.editUser(userSaveParam);
            return JsonResult.ok("修改成功");
        }

    }
    @GetMapping("/select")
    public JsonResult selectUser(UserQuery userQuery){
        log.debug("开始处理查询用户信息的请求");
        List<UserVO> userVOList = userService.selectUser(userQuery);
        log.debug("查询用户信息成功，用户信息为：{}",userVOList);
        return JsonResult.ok(userVOList);
    }
    @PostMapping("/resetPassword/{id}")
    public JsonResult resetPassword(@PathVariable Long id){
        log.debug("开始处理重置密码的请求");
        int rows = userService.resetPasswordSer(id);
        if(rows==0){
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
        return JsonResult.ok("重置密码成功");
    }
    @PostMapping("/updateStatus/{id}/{status}")
    public JsonResult updateStatus(@PathVariable Long id,@PathVariable String status){
        log.debug("开始处理修改用户状态的请求"+id+","+status);
        int rows = userService.updateStatus(id,status);
        if(rows==0){
            return new JsonResult(StatusCode.OPERATION_FAILED,"修改用户状态失败");
        }
        return JsonResult.ok("修改用户状态成功");
    }
    @PostMapping("/delete/{id}")
    public JsonResult deleteUser(@PathVariable Long id){
        log.debug("开始处理删除用户的请求");
        int rows = userService.deleteUser(id);
        if(rows==0){
            return new JsonResult(StatusCode.OPERATION_FAILED,"删除用户失败");
        }else{
            return JsonResult.ok("删除用户成功");
        }
    }

    @GetMapping("/select/audit/{parentId}")
    public JsonResult selectAuditUser(@PathVariable Long parentId){
        log.debug("开始处理查询审批人的请求{}",parentId);
        List<UserVO> userVOList = userService.selectAuditUser(parentId);
        log.debug("查询审批人信息成功，审批人信息为：{}",userVOList);
        return JsonResult.ok(userVOList);
    }

}
